package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.cloud.EMHttpClient;
import com.magicborrow.BaseLog;
import com.magicborrow.MagicBorrowApplication;
import com.magicborrow.R;
import com.magicborrow.beans.UserBean;
import com.magicborrow.utils.MD5;
import com.magicborrow.utils.UmengUtils;
import com.magicborrow.utils.UserTools;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;

/**
 * Created by River on 6/11/16.
 */
public class SplashActivity extends BaseActivity {

    private UserBean.User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
        setHasTitle(false);
        BaseLog.e("password", MD5.getMD5(MD5.getMD5("111111")));
        user = UserTools.getUser(this);

        // 一进来就开启推送

        if (UserTools.isLogin(this))
            EMChatManager.getInstance().login(user.getId() + "", user.getPassword(), new EMCallBack() {
                @Override
                public void onSuccess() {
                    //  Toast.makeText(TaPageActivity.this, "chenggong" + MD5.getMD5(MD5.getMD5(user.getPassword())), Toast.LENGTH_SHORT).show();
                    // 登陆成功，保存用户名密码
                    MagicBorrowApplication.getInstance().setUserName(user.getId() + "");
                    MagicBorrowApplication.getInstance().setPassword(user.getPassword());

                    // 更新当前用户的nickname 此方法的作用是在ios离线推送时能够显示用户nick
                    boolean updatenick = EMChatManager.getInstance().updateCurrentUserNick(
                            user.getNickname());
                    Log.e("xxxxx", "登录成功  " + user.getId() + "");
                }

                int t = 0;

                @Override
                public void onError(int i, String s) {
                    Log.e("环信登录失败", s);
                    if (t < 4) {
                        EMChatManager.getInstance().login(user.getId() + "", user.getPassword(), this);
                        t++;
                    }

                }

                @Override
                public void onProgress(int i, String s) {

                }
            });
        mHandler.postDelayed(goToMainActivity, SHOW_TIME_MIN);

    }

    private static final long SHOW_TIME_MIN = 1700;// 最小显示时间

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
//            switch (msg.what) {
//                default:
//                    break;
//            }
        }
    };
    //进入下一个Activity
    Runnable goToMainActivity = new Runnable() {

        @Override
        public void run() {
            Intent intent = new Intent();
            if (user == null || TextUtils.isEmpty(user.getMobile())) {
                intent.setClass(SplashActivity.this, FirstRegisterActivity.class);
            } else {

                intent.setClass(SplashActivity.this, MainActivity.class);
            }
            startActivity(intent);
            finish();
        }
    };
}
